var printPurchaseOrderTemplMixin = function (orderName) {
  return {
    mixins: [printCommonOrderMixin('searchOrders')],

    props: {
      orderList: {
        type: Array,
        required: true
      }
    },

    data: function () {
      return {
        templateOrderName: orderName,
        orders: [],
        searchOrders: [],
        orderTime: moment().valueOf(),
        deliveryTime: null,
        deliveryEdit: true,
        orderSearch: {
          materialName: '',
          curstomerId: '',
        },
        selectCurstomer: {},
        materialNameList: [],
        curstomerList: [],
        hasSumBtn: false,
        isShowSum: false,
        hasImgBtn: false,
        isShowImg: false,
        showEmail: false,
        addRowRemark: false,
        addDelRow: false,
        supplier: '',
        isUrgent:false,
        moment: moment
      }
    },

    created: function () {
      var self = this;
      this.setTemplConfig().then(function (config) {
        self.setPrintId();
        self.initTemplConfig(config);
        self.setOrders();
      }).catch(function () {});
    },

    methods: {
      initTemplConfig: function (config) {
        var columns = [];
        var columnName = '';
        switch (orderName) {
          case 'purchaseOrder':
            {
              var orderType = this.orderType;
              this.hasSumBtn = config.hasSumBtn;
              this.hasImgBtn = config.hasImgBtn;
              this.isShowImg = config.showTableImg
              var isShowSum;
              var groupFields = [];
              var mergeFields = [];
              if (orderType === 1) {
                isShowSum = config.showOrderTableSum;
                groupFields = config.orderGroup;
                mergeFields = config.orderMerge;
                columns = config.orderPurchaseColumns;
                columnName = 'orderPurchaseColumns';
              } else {
                isShowSum = config.showWarehouseTableSum;
                groupFields = config.warehouseGroup;
                mergeFields = config.warehouseMerge;
                columns = config.warehousePurchaseColumns;
                columnName = 'warehousePurchaseColumns';
              }
              this.isShowSum = isShowSum;
              this.groupFields = groupFields;
              this.mergeFields = mergeFields;
              this.showEmail = config.showEmail;
              this.addRowRemark = config.addRowRemark;
              this.addDelRow = config.addDelRow;
              break;
            }
          case 'prePurchaseOrder':
            {
              this.hasImgBtn = config.hasImgBtn;
              this.isShowImg = config.showTableImg
              columns = config.prePurchaseColumns;
              columnName = 'prePurchaseColumns';
              this.showEmail = config.showEmail;
              this.addRowRemark = config.addRowRemark;
              this.addDelRow = config.addDelRow;
              break;
            }
        }

        this.clauseContent = config.clauseContent;

        CommonTemplateUtil.setColumnConfig(columns, columnName);
        this.tableColumns = columns;
      },

      setOrders: function () {
        var orderList = this.orderList;
        orderList = this.getMergeOrders(orderList);
        orderList = _.cloneDeep(orderList);
        for (var i = 0; i < orderList.length; i++) {
          var order = orderList[i];
          if (orderName === 'prePurchaseOrder') {
            order.printOrderId = SysUtil.getId();
          }
          order._id = SysUtil.getId();
          var rowRemark = order.remark;
          if (SysUtil.isEmpty(rowRemark)) {
            rowRemark = '';
          }
          order.rowRemark = rowRemark;
          order._hasRow = false;
        }
        this.orders = orderList;
        this.initData();
        this.searchData();
      },

      getMergeOrders: function (orderList) {
        return orderList;
      },

      initData: function () {
        var orders = this.orders;
        var orderSearch = this.orderSearch;
        var materialNameList = this.getDropDown(orders, 'materialName');
        if (materialNameList.length > 0) {
          this.$set(orderSearch, 'materialName', materialNameList[0]);
        }
        this.materialNameList = materialNameList;

        var curstomerList = this.getDropDown(orders, 'curstomer');
        curstomerList = _.uniqBy(curstomerList, 'id');
        if (curstomerList.length > 0) {
          var selectCurstomer = curstomerList[0];
          this.selectCurstomer = selectCurstomer;
          this.$set(orderSearch, 'curstomerId', selectCurstomer.id);
        }
        this.curstomerList = curstomerList;

        var maxLaterPhaseList = this.getDropDown(orders, 'maxLaterPhase');
        var maxLaterPhase = _.max(maxLaterPhaseList);
        if (maxLaterPhase !== undefined) {
          this.deliveryTime = maxLaterPhase;
          this.deliveryEdit = false;
        } else {
          var proCycleList = this.getDropDown(orders, 'procurementCycles');
          var proCycle = _.max(proCycleList);
          if (proCycle !== undefined) {
            this.deliveryTime = moment().startOf('day').add(proCycle, 'day');
          }
          this.deliveryEdit = true;
        }
      },

      searchData: function () {
        var orders = this.orders;
        var orderSearch = this.orderSearch;
        var searchOrders = _.filter(orders, function (order) {
          return order.filterData(orderSearch);
        });
        this.searchOrders = searchOrders;
        this.sortData();
        if (orderName === 'purchaseOrder') {
          this.orderGroups = this.getOrderGroup();
        }
      },

      getOrderGroup: function () {
        return [];
      },

      getDropDown: function (orders, field, filterFun) {
        var list = _.map(orders, function (order) {
          return _.get(order, field);
        });
        list = _.uniq(list);
        list = _.filter(list, function (value) {
          if (filterFun === undefined) {
            return value !== undefined && value !== null;
          }
          return filterFun(value);
        });
        return list;
      },

      curstomerChange: function (curstomerId) {
        var curstomerList = this.curstomerList;
        var curstomer = _.find(curstomerList, {
          id: curstomerId
        });
        this.selectCurstomer = curstomer;
        this.searchData();
      },

      changeRowRemark: function (order) {
        var hasRow = order._hasRow;
        hasRow = !hasRow;
        order._hasRow = hasRow;
      },

      sortData: function () {
        var searchOrders = this.searchOrders;
        var tableColumns = this.tableColumns;
        var sortColumn = _.find(tableColumns, function (tableColumn) {
          return tableColumn.sortType !== undefined && tableColumn.sortType !== '';
        });
        var printOrders = _.clone(searchOrders);
        if (sortColumn !== undefined) {
          var sortType = sortColumn.sortType;
          sortType = sortType === 'ascending' ? 'asc' : 'desc';
          var sortConfig = sortColumn.config;
          var sortField = sortConfig.sortField;
          if (sortField === undefined) {
            sortField = sortConfig.field;
          }
          printOrders = SysUtil.sortDataByField(printOrders, sortField, sortType);
        }
        this.printOrders = printOrders;
      },

      toOrderData: function (tableColumn, type) {
        var sortType = tableColumn.sortType;
        if (sortType !== '') {
          if (sortType === type) {
            tableColumn.sortType = '';
          } else {
            tableColumn.sortType = type;
          }
        } else {
          tableColumn.sortType = type;
          var tableColumns = this.tableColumns;
          for (var i = 0; i < tableColumns.length; i++) {
            var column = tableColumns[i];
            if (!column.hasSort || column === tableColumn) {
              continue;
            }

            if (column.sortType !== '') {
              column.sortType = '';
              break;
            }
          }
        }
        this.sortData();
        if (this.templateOrderName === 'purchaseOrder') {
          this.orderGroups = this.getOrderGroup();
        }
      },

      urgentClick:function(){
        this.isUrgent = !this.isUrgent;
      }
    },

    components: {
      ShowTextarea: ShowTextarea
    }
  };
};